#include <sys_head.h>

typedef struct msgbuf
{
    long type;//消息的类型
    char buf[128];//消息的正文
}MSGBUF;

int main(int argc, char const *argv[])
{
    // ftok函数会把我们出入的两个参数通过某种算法生成一串秘钥
    key_t key = ftok("/home/student/桌面",1);
    //创建/打开消息队列
    int id = msgget(key,0666);
    //从消息队列中取出1000号消息，把消息正文存入参数二的buf中
    int pid = fork();
    while (1)
    {
        if (0 == pid)
        {
            MSGBUF msgbuf;
            msgrcv(id,&msgbuf,sizeof(msgbuf.buf),1,0);
            printf("收到消息为：%s\n",msgbuf.buf);
        }
        else if (0 < pid)
        {
            MSGBUF msgbuf;
            msgbuf.type = 2;
            printf("请输入消息内容\n");
            scanf("%s", msgbuf.buf);
            msgsnd(id, &msgbuf, sizeof(msgbuf.buf), 0);
        }
    }

    //释放消息队列给系统
    msgctl(id,IPC_RMID,NULL);
    return 0;
}
